package com.ltc.cdr.connector;
import it.sauronsoftware.ftp4j.FTPClient;

import it.sauronsoftware.ftp4j.FTPFile;

import java.io.File;



public class download {
 
	public String DownloadAction(String host,String User,String Password,String PathDownload,String DirectoryRemote,String Filter,String PathConvert,String Type,String StatusDay)
	{
		int x=0;
	try {
	
		
	FTPClient client = new FTPClient();	 
	client.connect(host, 21);		 
	client.login(User, Password);
	client.changeDirectory(DirectoryRemote);
	FTPFile[] list = client.list(Filter+"*");
	File file ;


		for(int i =0;i<list.length;i++){
			
			
		file =new File(PathDownload+"\\"+list[i].getName());
			//file =new File(PathConvert+"\\"+list[i].getName().substring(0, list[i].getName().length() -3)+"txt");
		
	
			
		if(!file.exists())
		{
		client.download(list[i].getName(), new java.io.File(PathDownload+"\\" +list[i].getName()));
		ConnvertFile(false,PathDownload+"\\" +list[i].getName(),PathConvert+"\\"+list[i].getName().substring(0, list[i].getName().length() -3)+"txt");
		x++;
		}
	
		
		}
	
	

	
	
	client.disconnect(true);
	
	
	}
	catch (Exception e) {
	
		e.printStackTrace();
	
		}
	  
	return "Download " + x + " Files  " +Type+ " Completed! of .... " + StatusDay;
	
	}
	
	private void ConnvertFile(Boolean Header,String InputFile, String OutFile)
	{
		
		try
		{
			if(Header==true)
			{
				 new ProcessBuilder("ConvertData\\4GSkip.exe",InputFile,OutFile).start();
			}
			else
			{
				 new ProcessBuilder("ConvertData\\4GSkip.exe","-noheader",InputFile,OutFile).start();
			}
		
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
			
		}
		
	}
	
	public static String CheckFile(String Path)
	{
		String Status="";
		
		try{
			
			 
			File file = new File(Path);
			if (!file.exists()) {
				if (file.mkdir()) {
				
					Status = Path +"--->directory is created" ;
				} else {
				
					Status = Path +"--->Failed to create directory!";
				}
			}
			
			
    	}catch(Exception e){
    		e.printStackTrace();
    	}
		
		return Status;
	}
	
	

}

